﻿using System;
using System.Collections.Generic;
using System.Text;
using LLFM.CAT.Model;
using Common;
using LLFM.CAT.Config;
using System.Data;
using System.Data.SqlClient;

namespace LLFM.CAT.Service
{
    public sealed class ProductsImagesService
    {
        /// <summary>
        /// 添加img
        /// </summary>
        /// <param name="imgUrl"></param>
        /// <param name="name"></param>
        /// <param name="productId"></param>
        /// <param name="sequence"></param>
        /// <param name="status"></param>
        public static void CreateImages(string midImageUrl, string smallImageUrl, string bigImageUrl, string name, int productId, int sequence, short status)
        {
            SqlHelper.ExecuteNonQuery(DBConfig.BitautoSDT
                , CommandType.StoredProcedure
                , "pro_CreateProductsImages"
                , new SqlParameter("@MidImageUrl", midImageUrl)
                , new SqlParameter("@SmallImageUrl", smallImageUrl)
                , new SqlParameter("@BigImageUrl", bigImageUrl)
                , new SqlParameter("@Name", name)
                , new SqlParameter("@ProductId",productId )
                , new SqlParameter("@CreateTime", DateTime.Now)
                , new SqlParameter("@Status", status)
                , new SqlParameter("@Sequence", sequence));
        }

        /// <summary>
        /// 通过ProductId获得img的列表
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static List<M_ProductsImages> GetProductImagesListByProductId(int productId, int status)
        {
            SqlParameter[] parms = {};
            DataTable dt = SqlHelper.ExecuteDataset(DBConfig.BitautoSDT,
                CommandType.StoredProcedure,
                "pro_GetProductImagesListByProductId"
                , new SqlParameter("@ProductId", productId)
                , new SqlParameter("@Status", status)).Tables[0];
            return ModelHelper.FillModelsFromDataTable<M_ProductsImages>(dt);
        }

        public static void DelImgs(List<M_ProductsImages> delImgs)
        {
            StringBuilder sql = new StringBuilder();
            foreach(M_ProductsImages img in delImgs )
            {
                sql.Append(string.Format("exec pro_UpdateProductsImageStatusById {0},{1};", img.Id,0)); 
            }
            if (sql.ToString() != string.Empty)
            {
                SqlHelper.ExecuteNonQuery(DBConfig.BitautoSDT, CommandType.Text, sql.ToString());
            }
        }

        public static void DelProductsImagById(int id)
        {
            int status = 0;
            SqlHelper.ExecuteNonQuery(
               DBConfig.BitautoSDT,
               CommandType.StoredProcedure,
               "pro_UpdateProductsImageStatusById",
               new SqlParameter("@Id", id),
                new SqlParameter("@Status", status)
               );
        }
    }
}
